<template>
    <el-drawer title="我是标题" :visible.sync="dialogVisible" :with-header="true" class="demand_box" size='600px' @close="resetForm('product1')">
        <div class="flexbox flexcolumn height_100">
            <div class="demand_head flexbox flexcenter flexbetween">
                <span class="demand_title">创建单个需求</span>
                <span class="icon_btn icon_btn_32"><img src="@/assets/img/close_check.svg" alt="" @click="resetForm('product1')"></span>
            </div>
            <div class="demand_center flex">
                <div>
                    <div class="part_title" data-require>产品信息</div>
                    <div class="part">
                        <el-form 
                        :model="addVideoSingleSzForm"
                        ref="product1"
                        label-width="110px"
                        label-position="left"
                        hide-required-asterisk="true"
                        class="dynamic">
                             <div class="flexbox flexcenter dualform">
                                <el-form-item label="品牌产品"  prop="brand_id" :rules="{ required: true, message: '品牌不能为空', trigger:['change','blur']}">
                                    <el-select v-model="addVideoSingleSzForm.brand_id" filterable placeholder="选择品牌" size="small" @change="addVideoSingleSzForm.product_id = ''">
                                    <el-option :label="item.brand_name" :value="item.brand_id" v-for="item in bplist" :key="item.brand_id"></el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item   prop="product_id" :rules="{ required: true, message: '产品不能为空', trigger: ['change','blur']}">
                                <el-select v-model="addVideoSingleSzForm.product_id" filterable placeholder="选择产品" size="small" :disabled="!addVideoSingleSzForm.brand_id" style="margin-left:12px;" >
                                    <el-option :label="item.product_name" :value="item.product_id" v-for="item in filterBplist(addVideoSingleSzForm.brand_id)" :key="item.product_id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </div>
                            <el-form-item label="投放渠道" prop="channel" :rules="{ required: true, message: '渠道不能为空', trigger: ['change','blur']}">
                                <el-select v-model="addVideoSingleSzForm.channel" placeholder="选择投放渠道" size="small" @change="addVideoSingleSzForm.channel != 1 && (addVideoSingleSzForm.business_scene = 5)">
                                <el-option label="千川" :value="1"></el-option>
                                <el-option label="UD" :value="2"></el-option>
                                <el-option label="巨量" :value="3"></el-option>
                                <el-option label="腾讯" :value="4"></el-option>
                                <el-option label="TK" :value="5"></el-option>
                                <el-option label="京易投" :value="6"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="业务场景"  prop="business_scene" :rules="{ required: true, message: '场景不能为空', trigger: 'blur'}">
                                <el-radio-group v-model="addVideoSingleSzForm.business_scene" size="mini">
                                    <el-radio-button :class="{is_radio_show:addVideoSingleSzForm.channel != 1}" label="5">短视频</el-radio-button>
                                    <el-radio-button label="6" v-if="addVideoSingleSzForm.channel == 1">直播</el-radio-button>
                                </el-radio-group>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
                <div>
                    <div class="part_title" data-require>人员信息</div>
                    <div class="part">
                        <el-form 
                        :model="addVideoSingleSzForm"
                        ref="product2"
                        label-width="110px"
                        label-position="left"
                        hide-required-asterisk="true"
                        class="dynamic">
                            <el-form-item label="平面" prop="designer_uid" :rules="{ required: true, message: '平面不能为空', trigger:['change','blur']}">
                                <el-select v-model="addVideoSingleSzForm.designer_uid" filterable placeholder="请选择平面" size="small" >
                                    <el-option :label="item.nickname" :value="item.id" v-for="item in optimiList" :key="item.id"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="核心创意人" prop="centre_uid" :rules="{ required: true, message: '核心创意人不能为空', trigger:['change','blur']}">
                                <el-select v-model="addVideoSingleSzForm.centre_uid" filterable placeholder="请选择核心创意人" size="small">
                                    <el-option label="默认剪辑" :value="0"></el-option>
                                    <el-option :label="item.nickname" :value="item.id" v-for="item in centreList" :key="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
                <div>
                    <div class="part_title" >需求信息</div>
                    <div class="part">
                        <el-form 
                        :model="addVideoSingleSzForm"
                        ref="product3"
                        label-width="110px"
                        label-position="left"
                        hide-required-asterisk="true"
                        class="dynamic">
                            <el-form-item label="截止日期" prop="closing_date" :rules="{ required: true, message: '截止日期不能为空', trigger:['change','blur']}">
                                <div class="flexbox" style="gap:12px">
                                    <div 
                                    class="date_day"
                                    :class="{'date_day_active':addVideoSingleSzForm.closing_date == item.value}"
                                    v-for="item in date_day" :key="item.value"  
                                    @click="addVideoSingleSzForm.closing_date = item.value">
                                    {{item.label}}
                                    </div>
                                </div>
                            </el-form-item>
                            <el-form-item label="需求信息" prop="description">
                                <el-input type="textarea" :rows="4" placeholder="请输入需求信息，非必填" v-model="addVideoSingleSzForm.description">
                                </el-input>
                            </el-form-item>
                    </el-form>
                    </div>
                </div>
                <!-- <el-form label-position="top" style="margin-top:-10px" :model="addVideoSingleSzForm" size="medium" :hide-required-asterisk='true' ref="addVideoSingleSzForm" :rules="addVideoSingleSzRules" class="dialog_form">
                    <el-form-item label="品牌/产品" prop="">
                        <el-row :gutter="12">
                            <el-col :span="12">
                                <el-form-item prop="brand_id">
                                    <el-select v-model="brandSelect" placeholder="请选择品牌" filterable value-key='brand_id' style="width:100%" @change="selectBrand">
                                        <el-option :label="item.brand_name+filters.chanelName(item.channel)" :value="item" v-for="item in brandList" :key="item.brand_id">
                                            <span>{{item.brand_name}}{{filters.chanelName(item.channel)}}</span>
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item prop="product_id">
                                    <el-select v-model="addVideoSingleSzForm.product_id" placeholder="请选择产品" filterable style="width:100%" :disabled="!addVideoSingleSzForm.brand_id" @change="selectProduct">
                                        <el-option :label="item.name" :value="item.product_id" v-for="item in productList" :key="item.product_id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form-item>
                    <el-form-item label="业务场景" prop="business_scene">
                        <el-select v-model="addVideoSingleSzForm.business_scene" :disabled="!addVideoSingleSzForm.product_id" placeholder="请选择业务场景" style="width:100%">
                            <el-option :label="item.scene_name" :value="item.business_scene" v-for="item in sceneList" :key="item.business_scene"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="剪辑" prop="editor_uid">
                        <el-select v-model="addVideoSingleSzForm.editor_uid" filterable placeholder="请选择剪辑" style="width:100%">
                            <el-option :label="item.nickname" :value="item.id" v-for="item in optimiList" :key="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="核心创意人" prop="centre_uid">
                        <el-select v-model="addVideoSingleSzForm.centre_uid" filterable placeholder="请选择核心创意人" style="width:100%">
                            <el-option label="默认剪辑" :value="0"></el-option>
                            <el-option :label="item.nickname" :value="item.id" v-for="item in centreList" :key="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="截止日期" prop="closing_date">
                        <el-select v-model="addVideoSingleSzForm.closing_date" placeholder="请选择该需求的截止日期" style="width:100%">
                            <el-option label="周一" :value="1"></el-option>
                            <el-option label="周二" :value="2"></el-option>
                            <el-option label="周三" :value="3"></el-option>
                            <el-option label="周四" :value="4"></el-option>
                            <el-option label="周五" :value="5"></el-option>
                            <el-option label="周六" :value="6"></el-option>
                            <el-option label="周日" :value="7"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="需求信息" prop="description">
                        <el-input type="textarea" :rows="3" placeholder="请输入需求信息" v-model="addVideoSingleSzForm.description">
                        </el-input>
                    </el-form-item>
                </el-form> -->
            </div>
            <div class="demand_footer flexbox flexcenter">
                <el-button plain size="small" class="el-button-width" @click="resetForm('product1')">取 消</el-button>
                <el-button type="primary" size="small" class="el-button-width" @click="submitForm('product1')" :loading="loading" :disabled='loading'>完成创建</el-button>
            </div>
        </div>
    </el-drawer>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            bplist:[],
            date_day:
            [{label:"周一",value:1},
             {label:"周二",value:2},
             {label:"周三",value:3},
             {label:"周四",value:4},
             {label:"周五",value:5},
             {label:"周六",value:6},
             {label:"周天",value:7}],
            loading: false,
            dialogVisible: false,
            brandSelect: {},
            brandList: [], //品牌列表
            productList: [], //产品列表
            optimiList: [], //优化师列表
            sceneList: [], //业务场景列表
            scriptList: [],
            centreList:[],//核心创意人
            addVideoSingleSzForm: {
                year: "",
                week: "",
                brand_id: "",
                product_id: "",
                channel: "",
                business_scene: "5",
                designer_uid: "",
                closing_date: "",
                description: "",
                centre_uid:0,
            },
            addVideoSingleSzRules: {
                brand_id: [
                    {
                        required: true,
                        message: "请选择品牌",
                        trigger: ["blur","change"],
                    },
                ],
                product_id: [
                    {
                        required: true,
                        message: "请选择产品",
                        trigger: ["blur","change"],
                    },
                ],
                business_scene: [
                    {
                        required: true,
                        message: "请选择业务场景",
                        trigger: ["blur","change"],
                    },
                ],
                centre_uid: [
                    {
                        required: true,
                        message: "请选择核心创意人",
                        trigger: ["blur","change"],
                    },
                ],
                designer_uid: [
                    {
                        required: true,
                        message: "请选择执行人",
                        trigger: ["blur","change"],
                    },
                ],
                closing_date: [
                    {
                        required: true,
                        message: "请选择截止日期",
                        trigger: ["blur","change"],
                    },
                ],
                description: [
                    {
                        required: true,
                        message: "请输入需求信息",
                        trigger: ["blur","change"],
                    },
                ],
            },
        };
    },
    created() {
        this.$bus.on("addPictureSingleSz", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.addVideoSingleSzForm.week = value.week;
            this.addVideoSingleSzForm.year = value.year;
            // this.getBrandList();
            this.getOptimization();
            this.getCentreList()
            this.getBplist()
        });
    },
    beforeDestroy() {
        this.$bus.off("addPictureSingleSz");
    },
    methods: {
        //获取品牌列表
        async getBplist(){
            const { data: res } = await this.$http.post("adminapi/brandv2/bpList",{limit:0});
            if (res.code !== 200) {
            return this.$message.error(res.msg);
            } else {
                this.bplist = res.data.list;
            }
        },
        //添加产品-品牌产品-产品选择器需要根据品牌取出数据
        filterBplist(id){
            if(!id)return;
            let arr =  this.bplist.find((e)=> e.brand_id == id)
            if(!arr)return;
            return arr.product;
        },
        //获取品牌列表
        async getBrandList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/brandList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.brandList = res.data;
            }
        },

        // 获取产品
        async getProductList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/productList",
                {
                    brand_id: this.addVideoSingleSzForm.brand_id,
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.productList = res.data;
            }
        },

        // 获取核心创意人列表
        async getCentreList() {
            const { data: res } = await this.$http.post(
                "adminapi/company/szCentreList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.centreList = res.data;
            }
        },

        // 获取业务场景
        async getSceneList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/sceneList",
                {
                    brand_id: this.addVideoSingleSzForm.brand_id,
                    product_id: this.addVideoSingleSzForm.product_id,
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.sceneList = res.data;
            }
        },

        // 获取优化师列表
        async getOptimization() {
            const { data: res } = await this.$http.post(
                "adminapi/company/szGraphicList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.optimiList = res.data;
            }
        },

        //品牌选择
        selectBrand() {
            this.addVideoSingleSzForm.brand_id = this.brandSelect.brand_id;
            this.addVideoSingleSzForm.channel = this.brandSelect.channel;
            this.addVideoSingleSzForm.product_id = "";
            this.addVideoSingleSzForm.business_scene = "5";
            this.getProductList();
        },

        // 产品选择
        selectProduct() {
            this.addVideoSingleSzForm.business_scene = "5";
            this.getSceneList();
        },

        // 提交
        submitForm(formName) {
            this.$refs.product1.validate((valid)=>{
                console.log(valid)
            })
            this.$refs.product2.validate((valid)=>{
                console.log(valid)
            })
            this.$refs.product3.validate((valid)=>{
                console.log(this.addVideoSingleSzForm)
            })
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/Demandszv2/pictureAdd",
                        this.addVideoSingleSzForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "创建成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$parent.getDataList(1);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.$refs.product2.resetFields();
            this.$refs.product3.resetFields();
            this.dialogVisible = false;
            this.brandSelect = {};
        },
    },
    components: {},
};
</script>

<style scoped>
/* /deep/ .el-form-item__content ,/deep/ .el-form-item__label{
    line-height: 32px;
} */
/deep/ .el-drawer__header{
    height: 0;
    width: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
}
/deep/ .el-form-item{
  margin-bottom: 12px;
}
/deep/ .el-form-item:last-child{
  margin-bottom: 0;
}
/deep/ .el-input__inner{
  width: 199px;
  border-radius: 8px;
}
/deep/ .el-form-item__label{
  color:#5C636C !important;
}
/deep/ .el-radio-button__inner{
      min-width: 70px;
}
/deep/ .el-radio-button:last-child .el-radio-button__inner{
    border-radius: 0 6px 6px 0;
}
/deep/ .el-radio-button:first-child .el-radio-button__inner{
    border-radius: 6px 0 0 6px;
}
 /deep/ .is_radio_show .el-radio-button__inner{
  border-radius: 6px !important;
}
/deep/ .el-form-item__error{
    padding-top: 0px;
}
/deep/ label[for="closing_date"]::after{
    content: '*';
    color: #ff3c31;
    margin-left: 4px;
}
.dualform >>> .el-form-item:last-child{
  margin-bottom: 12px;
}
.dualform >>> .el-form-item:last-child .el-form-item__content{
  margin-left: 0px !important;
}
.part_title{
    color: #191B1E;
    margin-bottom: 12px;
    font-size: 16px;
    font-weight: 500;
}
.part_title[data-require]::after{
    content: "*";
    color: #F53F3F;
    margin-left: 4px;
}
.part{  
    padding: 20px;
    border-radius: 12px;
    border: 1px solid #E8EAEC;
    margin-bottom: 20px;
}
.date_day{
    background: #F8F8F9;
    border: 1px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    text-align: center;
    padding: 6px 8px;
    line-height: 20px;
    width: 32px;
}
.date_day_active{
    border: 1px solid #165DFF;
    background: #E0E9FF;
}
</style>
